package com.infolab.aims.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.infolab.aims.common.AbstractController;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.common.exception.ResourceNotFoundException;
import com.infolab.aims.domain.Vehicle;
import com.infolab.aims.service.intf.AssetModelService;
import com.infolab.aims.service.intf.VehicleService;


@Controller
@RequestMapping("/vehicle")
@SessionAttributes(value=VehicleController.SESSION_SEARCH_CRITERIA)
public class VehicleController extends AbstractController {

	public static final String SESSION_SEARCH_CRITERIA ="searchCriteria";
	public static final String CONTEXT_PATH ="/vehicle";
	public static final String JSP_MAIN_FORM="vehicleForm";
	public static final String JSP_VENDOR_ASSET_FORM="vehicleAssetForm";
	public static final String JSP_MAIN_SEARCH="vehicleSearch";

	@Autowired
	VehicleService vehicleService;
	
	@Autowired
	AssetModelService assetModelService;

	@RequestMapping(value="create", method = RequestMethod.GET)
	public String createVehicle(Model model) {

		model.addAttribute(new Vehicle());
		return JSP_MAIN_FORM;
	}

	@RequestMapping(value="create",method=RequestMethod.POST)
	public String saveVehicle(@ModelAttribute Vehicle vehicle, BindingResult result,Model model,HttpServletRequest request) {

		if (validateVehicle(vehicle, result)) {	
			return JSP_MAIN_FORM;
		}

		vehicleService.createVehicle(vehicle);
		setSuccessMessage(getMessageResources("success.create","vehicle " + vehicle.getVehicleId()));

		return "redirect:"+CONTEXT_PATH+"/update/" + vehicle.getVehicleId();


	}

	@RequestMapping(value="update/{id}",method=RequestMethod.POST)
	public String updateVehicle(@PathVariable String id,@ModelAttribute Vehicle vehicle, BindingResult result,Model model,HttpServletRequest request) {

		if (validateVehicle(vehicle, result)) {
			return JSP_MAIN_FORM;
		}

		vehicleService.updateVehicle(vehicle);
		setSuccessMessage(getMessageResources("success.update","vehicle " + vehicle.getVehicleId()));

		//refreshGlobalVehicle(request);

		return "redirect:"+CONTEXT_PATH+"/update/" + vehicle.getVehicleId();	

	}

	@RequestMapping(value={"update/{id}","detail/{id}"}, method = RequestMethod.GET)
	public String detailVehicle(@PathVariable String id,Model model,HttpServletRequest request) {

		Vehicle vehicle = vehicleService.getVehicle(id);
		if(vehicle==null) {
			throw new ResourceNotFoundException("Vehicle " + id);
		}

		model.addAttribute(vehicle);
		return JSP_MAIN_FORM;

	}


	@ModelAttribute(VehicleController.SESSION_SEARCH_CRITERIA)
	public SearchCriteria getVehicleSearchCriteria()
	{
		return new SearchCriteria();
	}


	@RequestMapping(value="search")
	public String searchVehicle(@ModelAttribute(VehicleController.SESSION_SEARCH_CRITERIA) SearchCriteria searchCriteria,@ModelAttribute("currentSearchCriteria") SearchCriteria currentSearchCriteria,Model model) {

		if(currentSearchCriteria.getCurPage()!=null)
			searchCriteria.setCurPage(currentSearchCriteria.getCurPage());

		SearchResult searchResult = vehicleService.searchVehicle(searchCriteria);
		model.addAttribute(searchResult);

		return JSP_MAIN_SEARCH;
	}

	@RequestMapping(value={"delete/{id}"}, method = RequestMethod.GET)
	public String deleteVehicle(@PathVariable String id,Model model,HttpServletRequest request) {

		vehicleService.deleteVehicle(id);

		return "redirect:"+CONTEXT_PATH+"/search";
	}


	private boolean validateVehicle(Vehicle vehicle,BindingResult result)
	{
		validator.validate(vehicle, result);

		return result.hasErrors();
	}

	private boolean validateVehicleAssets(Vehicle vehicle,BindingResult result)
	{
		//		if(vehicle.getSection().getSectionId()==null)
		//			vehicle.setSection(null);

		//		validator.validate(vehicle, result);

		return result.hasErrors();
	}
}
